<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\store\controller;

use library\Controller;
use library\tools\Data;
use think\Db;
use app\store\model\Common;
/**
 * 套餐管理
 * Class Documentlist
 * @package app\store\controller
 */
class Package extends Controller
{
    /**
     * 指定数据表
     * @var string
     */
    protected $table = 'StorePackage';
    protected $table1 = 'store_package';

    /**
     * 套餐管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '套餐管理';
        $query = $this->_query($this->table)->like('title');
        $query->where(['is_deleted' => '0'])->order('sort desc,id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {

        foreach ($data as $k =>&$v){
            $v['total_price'] = Db::table("store_goods_cate")->where("id","in",$v['project_id'])->sum('price');
        }

    }



    /**
     * 添加套餐
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->title = '添加套餐';
        $this->brand_list = [["id"=>1,"title"=>"基因检测"],["id"=>2,"title"=>"疾病检测"],["id"=>3,"title"=>"健康检测"]];
        $this->cate_list = Db::table("store_goods_cate")->where("pid=1 and status=1 and is_deleted=0")->field("id,title")->select();
        $this->symptom_list = Db::table("store_goods_cate")->where("pid=11 and status=1 and is_deleted=0")->field("id,title")->select();
        $this->project_list = Db::table("store_goods_cate")->where("pid=30 and status=1 and is_deleted=0")->field("id,title,price")->select();
        $this->isAddMode = '1';
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑套餐
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->title = '编辑套餐';
        $this->brand_list = [["id"=>1,"title"=>"基因检测"],["id"=>2,"title"=>"疾病检测"],["id"=>3,"title"=>"健康检测"]];
        $this->cate_list = Db::table("store_goods_cate")->where("pid=1 and status=1 and is_deleted=0")->field("id,title")->select();
        $this->symptom_list = Db::table("store_goods_cate")->where("pid=11 and status=1 and is_deleted=0")->field("id,title")->select();
        $this->project_list = Db::table("store_goods_cate")->where("pid=30 and status=1 and is_deleted=0")->field("id,title,price")->select();
        $this->isAddMode = '0';
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    protected function _form_filter(&$data)
    {
        // 生成ID
        if (empty($data['id'])) $data['id'] = Data::uniqidNumberCode(14);
        if ($this->request->isGet()) {
            $this->catess = array(array('id'=>0,'title'=>'平台介绍'));
        } elseif ($this->request->isPost()) {
            if(!empty($data['cate_id'])){
                $data['cate_id'] = implode(',',$data['cate_id']);
                $cate_name = '';
                $cate_list = Db::table("store_goods_cate")->where("id","in",$data['cate_id'])->field("title")->select();
                foreach ($cate_list as $k =>&$v){
                    $cate_name .= $v['title'].',';
                }
                $data['cate_name'] = trim($cate_name,',');
            }
            if(!empty($data['symptom_id'])){
                $data['symptom_id'] = implode(',',$data['symptom_id']);
                $symptom_name = '';
                $symptom_list = Db::table("store_goods_cate")->where("id","in",$data['symptom_id'])->field("title")->select();
                foreach ($symptom_list as $k =>&$v){
                    $symptom_name .= $v['title'].',';
                }
                $data['symptom_name'] = trim($symptom_name,',');
            }
            if(!empty($data['project_id'])){
                $data['project_id'] = implode(',',$data['project_id']);
                $project_name = '';
                $project_list = Db::table("store_goods_cate")->where("id","in",$data['project_id'])->field("title")->select();
                foreach ($project_list as $k =>&$v){
                    $project_name .= $v['title'].',';
                }
                $data['project_name'] = trim($project_name,',');
            }

            if(!empty($data['brand_name'])){
                $data['brand_name'] = implode(',',$data['brand_name']);
            }
        }
    }

    /**
     * 表单结果处理
     * @param boolean $result
     */
    protected function _form_result($result)
    {
        if ($result && $this->request->isPost()) {
            $this->success('编辑成功！', 'javascript:history.back()');
        }
    }

    /**
     * 启用套餐
     * @auth false
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->_save($this->table, ['status' => '1']);
    }

    /**
     * 禁用套餐
     * @auth false
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->_save($this->table, ['status' => '0']);
    }



    /**
     * 删除套餐
     * @auth false
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

}
